package com.bupt.godxian.charging.controller;


import com.bupt.godxian.charging.config.CommonResponse;
import com.bupt.godxian.charging.entity.Charger;
import com.bupt.godxian.charging.entity.dto.ChargerStatus;
import com.bupt.godxian.charging.service.AdminService;
import com.bupt.godxian.charging.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/admin")
public class AdminController {


    private final AdminService adminService;

    @Autowired
    public AdminController(AdminService adminService) {
        this.adminService = adminService;
    }

    @GetMapping("/query_report")
    public ResponseEntity<?> queryReport(){

        // TODO(3): 处理，获取充电站的数据报表列表
        //Service操作
        CommonResponse result=adminService.query_report();
        return ResponseEntity.ok(result);

    }

    @GetMapping("/query_all_piles_stat")
    public ResponseEntity<Map<String, Object>> queryAllPilesStat() {

        // 读取数据库
        List<Map<String, Object>> statList = new ArrayList<>();
        List<Charger> chargers = adminService.findChargeOrder();
        for (Charger charger : chargers) {
            Map<String, Object> stat = new HashMap<>();
            stat.put("pile_id", charger.getId());
            stat.put("status", charger.getChargerStatus());
            stat.put("cumulative_usage_times", charger.getCumulativeUsageTimes());
            stat.put("cumulative_charging_time", charger.getCumulativeChargingTime());
            stat.put("cumulative_charging_amount", charger.getCumulativeChargingAmount());
            statList.add(stat);
        }
        boolean success ;
        String errorMsg = null;
        if (chargers.isEmpty()) {
            success = false;
            errorMsg = "无充电桩";
        }
        else{
            success=true;
        }
        Map<String, Object> responseBody = new HashMap<>();
        if (success) {
            responseBody.put("code", 0);
            responseBody.put("message", "Success");
            responseBody.put("data", statList);

        }else {
            responseBody.put("code", -1);
            responseBody.put("message", errorMsg);
            responseBody.put("data", null);
        }
        return ResponseEntity.ok(responseBody);


    }

    @GetMapping("/query_queue")
    public ResponseEntity<?> queryQueue() {


        // TODO(3): 处理，获取目前所有正在排队的用户
        //Service操作
        CommonResponse result=adminService.query_queue();
        return ResponseEntity.ok(result);
    }

    @PostMapping("/update_pile")
    public ResponseEntity<?> updatePile(@RequestBody Map<String, Object> request) {
        // 获取充电桩参数
        String pile_id = (String) request.get("pile_id");
        ChargerStatus status = (ChargerStatus) request.get("status");

        // TODO(3): 处理，获取充电站的数据报表列表
        //Service操作
        CommonResponse result=adminService.update_pile(pile_id,status);
        return ResponseEntity.ok(result);
    }


}
